package com.baizhi.utils.spring;

import com.baizhi.utils.spring.bean.Male;
import org.junit.Test;
import org.springframework.util.ReflectionUtils;

public class ReflectionUtilsTest {

    /**
     * 反射相关
     */
    @Test
    public void test1() throws NoSuchFieldException {
        // 获取本类以及父类的所有属性
        ReflectionUtils.doWithFields(Male.class, field -> {
            System.out.println(field);
        });
        // 获取本类以及父类的所有方法
        ReflectionUtils.doWithMethods(Male.class, method -> {
            System.out.println(method.getDeclaringClass().getName() + " : " + method.getName());
        });
        // 获取对象的属性值
        Male male = new Male(1L, "张三", 20);
        ReflectionUtils.doWithFields(Male.class, field -> {
            field.setAccessible(true);
            Object fieldValue = ReflectionUtils.getField(field, male);
            System.out.println(fieldValue);
        });
    }
}

